﻿using Neoit.Utils.Extensions;

namespace Test
{
    public static class MockHelper
    {
        /// <summary>
        /// mock实体数据,设置集合中属性的值：string = propertyName,int = 1;bool=false
        /// </summary>
        public static T CustomMocks<T>(this T t, IEnumerable<string> list) where T : new()
        {
            if (t == null || !list.Has()) return t;

            foreach (var item in list)
            {
                var pi = t.GetType().GetProperty(item);

                if (pi.PropertyType == typeof(string))
                {
                    pi.SetValue(t, $"$_{item}", null);
                }
                if (pi.PropertyType == typeof(bool))
                {
                    pi.SetValue(t, false, null);
                }
                if (pi.PropertyType == typeof(int))
                {
                    pi.SetValue(t, 1, null);
                }
                if (pi.PropertyType.IsEnum)
                {
                    var enumValues = Enum.GetValues(pi.PropertyType);
                    if (enumValues.Length > 1)
                    {
                        var secondEnumValue = enumValues.GetValue(1);
                        var enumValue = Enum.ToObject(pi.PropertyType, secondEnumValue);
                        pi.SetValue(t, enumValue);
                    }
                }

            }
            return t;
        }
        /// <summary>
        /// 对自定义Mock的对象值进行断言
        /// </summary>
        public static void AssertCustomMocks<T>(this T t, IEnumerable<string> list) where T : new()
        {
            if (t == null || !list.Has()) return;
            foreach (var item in list)
            {
                var pi = t.GetType().GetProperty(item);

                if (pi.PropertyType == typeof(string))
                {
                    Assert.Equal($"$_{item}", pi.GetValue(t));
                }
                if (pi.PropertyType == typeof(bool))
                {
                    Assert.Equal(false, pi.GetValue(t));
                }
                if (pi.PropertyType == typeof(int))
                {
                    Assert.Equal(1, pi.GetValue(t));
                }
                if (pi.PropertyType.IsEnum)
                {
                    var enumValues = Enum.GetValues(pi.PropertyType);
                    if (enumValues.Length > 1)
                    {
                        var secondEnumValue = enumValues.GetValue(1);
                        var enumValue = Enum.ToObject(pi.PropertyType, secondEnumValue);
                        Assert.Equal(enumValue, pi.GetValue(t));
                    }
                }
            }
        }

        public static void AssertDefaultValue<T>(this T t, IEnumerable<string> list) where T : new()
        {
            if (t == null || !list.Has()) return;
            foreach (var item in list)
            {
                var pi = t.GetType().GetProperty(item);

                if (pi.PropertyType == typeof(string))
                {
                    Assert.Equal(default(string), pi.GetValue(t));
                }
                if (pi.PropertyType == typeof(bool))
                {
                    Assert.Equal(default(bool), pi.GetValue(t));
                }
                if (pi.PropertyType == typeof(int))
                {
                    Assert.Equal(default(int), pi.GetValue(t));
                }
                if (pi.PropertyType.IsEnum)
                {
                    var enumValues = Enum.GetValues(pi.PropertyType);
                    if (enumValues.Length > 1)
                    {
                        var secondEnumValue = enumValues.GetValue(0);
                        var enumValue = Enum.ToObject(pi.PropertyType, secondEnumValue);
                        Assert.Equal(enumValue, pi.GetValue(t));
                    }
                }
            }
        }
    }
    //todo:
    /*
     * CustomMocks(MockPolicy mockPolicy) 
     * MockPolicy{ 
     * default: string = propertyName,int = 1;bool=false
     * }
     */
}
